package com.github.androiddevelopmentframework.db;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.androiddevelopmentframework.R;
import com.github.freewu.commons.db.DBUtils;

import org.greenrobot.greendao.database.Database;

/**
 * Created by Administrator on 2017/2/8 0008.
 */
public class DBActivity extends AppCompatActivity{
    private Button btnDB1;
    private Button btnDB2;
    private TextView textDB;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_db_layout);

        btnDB1=(Button)findViewById(R.id.button_db1);
        btnDB2=(Button)findViewById(R.id.button_db2);
        textDB=(TextView)findViewById(R.id.text_db);

        btnDB1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Database db1= DBUtils.getInstance("db2").getDateBase();

                DaoMaster daoMaster=new DaoMaster(db1);

                User user=new User();
                user.setName("free");
                user.setSex(1);

                UserDao.createTable(db1,true);

                daoMaster.newSession()
                        .insertOrReplace(user);

                User getUser=daoMaster.newSession()
                        .getUserDao()
                        .queryBuilder()
                        .where(UserDao.Properties.Name.eq("free"))
                        .unique();

                textDB.setText(getUser.toString());
            }
        });

        btnDB1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Database db1=DBUtils.getInstance("db1").getDateBase();

                DaoMaster daoMaster=new DaoMaster(db1);

                User getUser=daoMaster.newSession()
                        .getUserDao()
                        .queryBuilder()
                        .where(UserDao.Properties.Name.eq("free"))
                        .unique();

                textDB.setText(getUser.toString());
            }
        });
    }
}
